package com.inspinia.base.api.controller;

import com.inspinia.base.api.model.InterfaceUser;
import com.inspinia.base.api.service.InterfaceUserService;
import com.inspinia.base.common.model.*;
import com.inspinia.base.pagination.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
* 的Controller
* Created by pujw on 2017-6-30.
*/
@Controller
@RequestMapping("apiManager/interfaceUser")
@Menu(name = "接口用户管理", roles = RolesEnum.vosInterfaceManager, icon = "icon-standard-page-white-go", parentName = "接口管理", openUrl = "paging")
public class InterfaceUserController {

    @Resource
    private InterfaceUserService interfaceUserService;

    /**
    * 跳转分页页面
    *
    * @return
    */
    @SubmenuOrResource(name = "跳转分页页面", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @RequestMapping(value = "paging", method = RequestMethod.GET)
    public String toPaginationPage() {
        return "interfaceUser/interfaceUser-pagination";
    }

    /**
    * 分页查询
    *
    * @return
    */
    @SubmenuOrResource(name = "分页查询", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "paging", method = RequestMethod.POST)
    public AjaxResult paging(InterfaceUser interfaceUser) {
        Pager<InterfaceUser> page = interfaceUserService.findPage(interfaceUser);
        return new AjaxResult(true, page);
    }

    /**
    * 创建
    *
    * @param interfaceUser
    * @return
    */
    @SubmenuOrResource(name = "创建", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult create(@Valid InterfaceUser interfaceUser) {
        interfaceUserService.insert(interfaceUser);
        return new AjaxResult(true);
    }

    /**
    * 根据id获取
    *
    * @param id id
    * @return
    */
    @SubmenuOrResource(name = "根据id获取", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public AjaxResult get(long id) {
        InterfaceUser bean = interfaceUserService.get(id);
        return new AjaxResult(true, bean);
    }

    /**
    * 编辑
    *
    * @param interfaceUser
    * @return
    */
    @SubmenuOrResource(name = "编辑", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "goEdit", method = RequestMethod.POST)
        public AjaxResult edit(@Valid InterfaceUser interfaceUser) {
        interfaceUserService.update(interfaceUser);
        return new AjaxResult(true);
    }

    /**
    * 删除
    *
    * @param id
    * @return
    */
    @SubmenuOrResource(name = "删除", roles = RolesEnum.vosInterfaceManager, type = ResourcesType.resources)
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public AjaxResult delete(Long id) {
        int deleted = interfaceUserService.delete(id);
        if( deleted > 0 ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"删除失败，请重试！");
    }

    /**
    * 批量删除
    *
    * @param ids
    * @return
    */
    @ResponseBody
    @RequestMapping(value = "batchDelete", method = RequestMethod.POST)
    public AjaxResult delete(@RequestParam(name = "ids[]", required = false, defaultValue = "") Long[] ids) {
        int deleted =  interfaceUserService.batchDelete(ids);
        if( deleted > 0 && ids.length == deleted ){
        	return new AjaxResult(true);
        }
        return new AjaxResult(false,"有"+(ids.length - deleted)+"个资源删除失败，请重试！");
    }

    /**
     * 修改user的激活状态
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateUserActiveStatus",method = RequestMethod.POST)
    public AjaxResult updateUserActiveStatus(Long userId){
        if (userId == null){
            return new AjaxResult(false,"id为空");
        }
        InterfaceUser user = interfaceUserService.get(userId);
        if (user.isEnable()){
            user.setEnable(false);
        }else {
            user.setEnable(true);
        }
        int update = interfaceUserService.update(user);
        if (update>0){
            return new AjaxResult(true);
        }
        return new AjaxResult(false);
    }



}